﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MiniCrawler.Entity;

namespace MiniCrawler.DataAccess
{
    public class ConfigHelper
    {
        // Thread helpers
        public Thread GetThreadById(int id)
        {
            var db = new CrwlDataEntities();
            var thread = (from t in db.Threads where t.Id == id select t).FirstOrDefault();
            if (thread != null) return thread;
            else return null;
        }

        public Thread GetFirstThread()
        {
            var db = new CrwlDataEntities();
            var thread = (from t in db.Threads select t).FirstOrDefault();
            if (thread != null) return thread;
            else return null;
        }

        public IEnumerable<Thread> GetAllThreads()
        {
            var db = new CrwlDataEntities();
            var threads = (from t in db.Threads select t);
            if (threads != null) return threads;
            else return null;
        }

        public int AddNewThread(string threadName, string hostName)
        {
            var db = new CrwlDataEntities();
            var thread = new Thread {Name = threadName, HostName = hostName, IsEnabled = true, State = true};
            db.Threads.Add(thread);
            return db.SaveChanges();
        }
        public int AddNewCategory(Thread thread, string categoryName, string urlExpression)
        {
            var db = new CrwlDataEntities();

            var t1 = (from t in db.Threads where t.Id == thread.Id select t).First();

            //var t = GetThreadById(thread.Id);
            var cat = new CategoryExpression { Name = categoryName, UrlExpression = urlExpression, Level = 1, Priority = 1, IsEnabled = true};
            //var cat = new CategoryExpression();         
            cat.Thread = t1;

            db.CategoryExpressions.Add(cat);

            //var test = new Thread();
            //thread.CategoryExpressions.Add(cat);
            //db.Entry(thread).State = EntityState.Modified;
            return db.SaveChanges();
        }


        public int DeleteCategory(int categoryId)
        {
            var db = new CrwlDataEntities();

            var t1 = (from t in db.CategoryExpressions where t.Id == categoryId select t).First();

            db.CategoryExpressions.Remove(t1);

            //var test = new Thread();
            //thread.CategoryExpressions.Add(cat);
            //db.Entry(thread).State = EntityState.Modified;)
            return db.SaveChanges();
        }


        public int DeleteData(int dataId)
        {
            var db = new CrwlDataEntities();

            var t1 = (from t in db.DataExpressions where t.Id == dataId select t).First();

            db.DataExpressions.Remove(t1);

            //var test = new Thread();
            //thread.CategoryExpressions.Add(cat);
            //db.Entry(thread).State = EntityState.Modified;)
            return db.SaveChanges();
        }


        public int UpdateCategoryPattern(int categoryId, string pattern)
        {
            var db = new CrwlDataEntities();

            var t1 = (from t in db.CategoryExpressions where t.Id == categoryId select t).First();
            t1.UrlExpression = pattern;

            db.Entry(t1).State = EntityState.Modified;

            //var test = new Thread();
            //thread.CategoryExpressions.Add(cat);
            //db.Entry(thread).State = EntityState.Modified;)
            return db.SaveChanges();
        }

        public int UpdateDataPattern(int dataId, string pattern)
        {
            var db = new CrwlDataEntities();

            var t1 = (from t in db.DataExpressions where t.Id == dataId select t).First();
            t1.UrlExpression = pattern;

            db.Entry(t1).State = EntityState.Modified;

            //var test = new Thread();
            //thread.CategoryExpressions.Add(cat);
            //db.Entry(thread).State = EntityState.Modified;)
            return db.SaveChanges();
        }


        public int AddNewData(Thread thread, string dataName, string urlExpression)
        {
            var db = new CrwlDataEntities();

            var t1 = (from t in db.Threads where t.Id == thread.Id select t).First();

            //var t = GetThreadById(thread.Id);
            var cat = new DataExpression { Name = dataName, UrlExpression = urlExpression, Level = 1, Priority = 1, IsEnabled = true};
            //var cat = new CategoryExpression();         
            cat.Thread = t1;

            db.DataExpressions.Add(cat);

            //var test = new Thread();
            //thread.CategoryExpressions.Add(cat);
            //db.Entry(thread).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public IEnumerable<CategoryExpression> GetCategoryExpressionesByThreadId(int threadId)
        {
            var db = new CrwlDataEntities();
            var threads = (from cExps in db.CategoryExpressions where cExps.Thread.Id == threadId select cExps);
            if (threads != null) return threads;
            else return null;
        }

        public IEnumerable<DataExpression> GetDataExpressionesByThreadId(int threadId)
        {
            var db = new CrwlDataEntities();
            var dataExps = (from dExps in db.DataExpressions where dExps.Thread.Id == threadId select dExps);
            if (dataExps != null) return dataExps;
            else return null;
        }


    }
}
